<?php

namespace App\Admin\Controllers;

use App\Models\Staff;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class StaffController extends Controller
{
    use HasResourceActions;

    const TITLE = '员工管理';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(self::TITLE)
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(self::TITLE)
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(self::TITLE)
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(self::TITLE)
            ->description(' ')
            ->body($this->form());
    }

	public function destroy($id)
	{
		$staff = Staff::findOrFail($id);
		if (Staff::ADMIN == $staff->genre) {
			return failed('管理员暂不可删除');
		}
		// TODO:: 其他验证
		if ($this->form()->destroy($id)) {
			$data = [
				'status'  => true,
				'message' => trans('admin.delete_succeeded'),
			];
		} else {
			$data = [
				'status'  => false,
				'message' => trans('admin.delete_failed'),
			];
		}

		return response()->json($data);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Staff);
        $grid->disableFilter();
        $grid->disableExport();

        $grid->id('ID')->sortable();
		$grid->genre('类型')->using(Staff::GENRES);
		$grid->name('姓名');
		$grid->mobile('手机');
		$grid->wechat('微信号');
        $grid->created_at('创建时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Staff::findOrFail($id));

		$show->id('ID');
		$show->genre('类型')->using(Staff::GENRES);
		$show->name('姓名');
		$show->mobile('手机');
		$show->wechat('微信号');
		$show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Staff);

		$form->disableViewCheck();
		$form->disableEditingCheck();
		if ($id) {
			$form->disableCreatingCheck();
		}

        $form->radio('genre', '类型')->options(Staff::GENRES)->default(Staff::GENERAL)->rules('required');
        $form->text('name', '姓名')->rules('required|string|max:8');
		$form->mobile('mobile', '手机')->rules('required');
		$form->text('wechat', '微信号')->rules('required|string|max:32');

        return $form;
    }
}
